package com.jwsoft.manager.core.integration.common.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.bifang.assistant.common.vo.SmsVO;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.bifang.module.system.core.integration.SmsIntegration;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

//短信发送接口
@Service("eduSmsIntegration")
public class EduSmsIntegrationImpl implements SmsIntegration {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    CommonIntegration commonIntegration;
    @Override
    public boolean send(SmsVO vo) {
        CommonVO commonSendVO = new CommonVO();
        commonSendVO.setParamKey("DYEDU_SMS_KEY");
        commonSendVO.setAreaCode("330783");
        String key=commonIntegration.getValueByKeyAndArea(commonSendVO);
        if(StringUtils.isBlank(key)){
            throw new AppException("短信必要参数未配置");
        }
        CommonVO commonUidVO = new CommonVO();
        commonUidVO.setParamKey("DYEDU_SMS_UID");
        commonUidVO.setAreaCode("330783");
        String uid=commonIntegration.getValueByKeyAndArea(commonUidVO);
        if(StringUtils.isBlank(uid)){
            throw new AppException("短信必要参数未配置");
        }
        CommonVO commonUrlVO = new CommonVO();
        commonUrlVO.setParamKey("DYEDU_SMS_URL");
        commonUrlVO.setAreaCode("330783");
        String url=commonIntegration.getValueByKeyAndArea(commonUrlVO);
        if(StringUtils.isBlank(url)){
            throw new AppException("短信必要参数未配置");
        }
        Map<String,Object> params=new HashMap<>();
        params.put("Uid",uid);
        params.put("Key",key);
        params.put("smsMob",vo.getPhone());
        params.put("smsText",vo.getMessage());
        try {
            String body=HttpUtil.post(url,params,3000);
            if(Integer.parseInt(body)>0){
                return true;
            }
            throw new AppException("发送短信失败:"+body);
        }catch (Exception e){
            logger.error("发送短信失败",e);
            logger.error("发送短信接口调用失败");
            return false;
        }
    }
}
